﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms.Integration;

namespace comm.editor
{
    public class DefEquipEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var elHost = new ElementHost
                {
                    Width = 460,
                    Height = 460,
                    Child = new UCDefEquip((comm.objst.IDefEquip)context.Instance)
                };
                edSvc.DropDownControl(elHost);
                return value;
            }
            return value;
        }
    }

    /// <summary>
    /// UCEquipType.xaml 的交互逻辑
    /// </summary>
    public partial class UCDefEquip : UserControl
    {
        comm.objst.IDefEquip _defEquip = null;

        public UCDefEquip(comm.objst.IDefEquip defEquip)
        {
            InitializeComponent();

            _defEquip = defEquip;

            Fill();
        }

        void ck_Checked(object sender, RoutedEventArgs e)
        {
            var ck = (CheckBox)sender;
            var type = (comm.objst.ItemEquipType)ck.Tag;
            if (ck.IsChecked == true)
            {
                var subs = comm.objst.DItem.GetSubEquipTypeList(type);
                var parent = comm.objst.DItem.GetParentEquipType(type);
                foreach (var item in subs)
                {
                    _defEquip.CanEquipTypeList.Remove(item);
                }
                _defEquip.CanEquipTypeList.Add(type);

                if (parent.HasValue)
                {
                    _defEquip.CanEquipTypeList.Remove(parent.Value);
                }
            }
            else
            {
                _defEquip.CanEquipTypeList.Remove(type);
            }
            Fill();
        }

        void Fill()
        {
            spList.Children.Clear();

            var names = comm.EnumHelper.GetNames(typeof(comm.objst.ItemEquipType));
            var dic = new Dictionary<comm.objst.ItemEquipType, string>();

            foreach (var name in names)
            {
                var grid = new Grid { Height = 26 };

                var type = comm.EnumHelper.Parse<comm.objst.ItemEquipType>(name);
                var isParent = comm.objst.DItem.IsParentEquipType(type);
                var parent = comm.objst.DItem.GetParentEquipType(type);
                var sel = _defEquip.CanEquipTypeList.Contains(type);
                var subSel = parent.HasValue && _defEquip.CanEquipTypeList.Contains(parent.Value);

                #region ck
                var ck = new CheckBox
                {
                    Tag = type,
                    Focusable = false,
                    IsChecked = sel,
                    Opacity = sel ? 1 : .5,
                    Width = 220,
                    Content = new TextBlock
                    {
                        Text = name,
                        FontSize = 14,
                        FontWeight = subSel ? FontWeights.Bold : FontWeights.Normal
                    },
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Center
                };
                ck.Checked += new RoutedEventHandler(ck_Checked);
                ck.Unchecked += new RoutedEventHandler(ck_Checked);
                grid.Children.Add(ck);
                #endregion

                if (!isParent && (sel || subSel))
                {
                    dic.Add(type, "");
                    if (!_defEquip.DefEquipDic.ContainsKey(type))
                    {
                        _defEquip.DefEquipDic.Add(type, "");
                    }

                    #region cb
                    var items = DefineFile.LastInstance.GetAllItem(type);
                    var cb = new ComboBox
                    {
                        Width = 160,
                        Margin = new Thickness(220, 0, 0, 0),
                        HorizontalAlignment = HorizontalAlignment.Left,
                        VerticalAlignment = VerticalAlignment.Center
                    };
                    cb.Items.Add(new TextBlock
                    {
                        Text = items.Length > 0 ? "items(" + items.Length.ToString() + ")" : "",
                        Opacity = .5
                    });
                    foreach (var itemId in items)
                    {
                        var item = DefineFile.LastInstance.GetItem(itemId);
                        var tb = new TextBlock
                        {
                            Tag = itemId,
                            Text = itemId
                        };
                        cb.Items.Add(tb);

                        if (_defEquip.DefEquipDic[type] == itemId)
                        {
                            if (DefineFile.LastInstance.GetItem(itemId) != null)
                            {
                                cb.SelectedItem = tb;
                            }
                        }
                    }
                    if (cb.SelectedIndex == -1)
                    {
                        cb.SelectedIndex = 0;
                    }

                    cb.SelectionChanged += new SelectionChangedEventHandler((o, e) =>
                    {
                        var tb = (TextBlock)cb.SelectedItem;
                        _defEquip.DefEquipDic[type] = tb.Tag == null ? "" : tb.Tag.ToString();
                    });
                    grid.Children.Add(cb);
                    #endregion
                }

                spList.Children.Add(grid);
            }

            foreach (var key in _defEquip.DefEquipDic.Keys)
            {
                if (dic.ContainsKey(key))
                {
                    dic[key] = _defEquip.DefEquipDic[key];
                }
            }
            _defEquip.DefEquipDic.Clear();
            foreach (var key in dic.Keys)
            {
                _defEquip.DefEquipDic.Add(key, dic[key]);
            }
        }
    }
}
